//
//  WMFoodsHeadView.m
//  witMorning
//
//  Created by Mac mini on 2018/1/3.
//  Copyright © 2018年 zt. All rights reserved.
//

#import "WMFoodsHeadView.h"
#import "WMFoodsHeadCell.h"

@implementation WMFoodsHeadView

-(void)setDateArray:(NSArray *)dateArray{
    _dateArray = dateArray;
    [self reloadData];
   
    dispatch_async(dispatch_get_main_queue(), ^{
        [self selectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
        
    });
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
         UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.sectionHeadersPinToVisibleBounds = YES;
        self = [super initWithFrame:frame collectionViewLayout:layout];
        self.delegate = self;
        self.dataSource = self;
        self.bounces = NO;
        self.showsHorizontalScrollIndicator = NO;
            [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        //    [layout setHeaderReferenceSize:CGSizeMake(SCREEN_W, 200)];
        
        [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headIdentifier"];
        [self registerClass:[WMFoodsHeadCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
//    forCellWithReuseIdentifier:@"cellIdentifier"];
        
        
    }
    return self;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dateArray?_dateArray.count:0;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(self.frame.size.height+15, self.frame.size.height);
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(self.frame.size.height+40, self.frame.size.height);
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
       
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headIdentifier" forIndexPath:indexPath];
        view.backgroundColor = PUBLIC_DARK;
        view.layer.borderColor = [UIColor lightGrayColor].CGColor;
        view.layer.borderWidth = 1;
        
        UILabel *lab = [[UILabel alloc]initWithFrame:view.frame];
//        lab.backgroundColor = PUBLIC_DARK;
        lab.textColor = PUBLIC_BLUE;
        lab.textAlignment = NSTextAlignmentCenter;
        lab.text = [NSString stringWithFormat:@"%@月",[WMTool currrentWithFormatterStyle:@"MM"]];
        [view addSubview:lab];
        return view;
    }


    return nil;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//    return UIEdgeInsetsMake(10, 20, 0, 20);
        return UIEdgeInsetsZero;
}
//
//设置水平间距 (同一行的cell的左右间距）
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
//垂直间距 (同一列cell上下间距)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}



-  (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WMFoodsHeadCell *cell = [self dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
//    cell.selected = NO;
    cell.lab.text = [NSString stringWithFormat:@"%@号",_dateArray[indexPath.item]];
//    cell..text = @"xxxx";
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.FoodHeadCallBack) {
        self.FoodHeadCallBack(indexPath.item);
    }
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
